package com.example.tsproject.fliter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;

import java.io.IOException;
import java.net.http.HttpResponse;

/**
 * @BelongsProject: springbootdemo
 * @BelongsPackage: com.example.springbootdemo.fliter
 * @Author: YoungDra
 * @CreateTime: 2024/08/06  14:22
 */
@WebFilter(urlPatterns = "/get")
//这里只过滤get请求
public class DemoFliter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
//        Filter.super.init(filterConfig);
        System.out.println("initial");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req= (HttpServletRequest) servletRequest;
        HttpResponse reps= (HttpResponse) servletResponse;
        String url=req.getRequestURI();
        System.out.println(url);
        if (url.contains("login")){
            System.out.println("logining");
            filterChain.doFilter(servletRequest,servletResponse);
        }
        else {
            String token =req.getHeader("token");
            if (token==null||token.equals(""))
                System.out.println("为登录");
//            else if()

        }
        //放行
        //TODO 目前都先直接放行，根据后面逻辑修改
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {
//        Filter.super.destroy();
        System.out.println("destroy");
    }
}
